// 客户详情控制器
define('mainMainKhxxyControler', ['angular'], function (angular) {
    var module = angular.module('controllers');
    module.controller('mainMainKhxxyControler', ['$scope','$location','$rootScope','$route','$routeParams','$filter','businessServers','noticeServers','followRecordsServers',function ($scope,$location,$rootScope,$route,$routeParams,$filter,businessServers,noticeServers,followRecordsServers){
        var businessId=$routeParams.id;
        $scope.currentData={};
        $scope.business_status=0;
        $scope.noticeData={
            user_id:$rootScope.user.id
        };
        businessServers.getBusinessById(businessId).then(function(data){
            $scope.currentData=data[0];
            $scope.formData=$scope.currentData;
            $scope.currentData.follow_up_records=angular.fromJson($scope.currentData.follow_up_records);
            $scope.noticeData.business_id=$scope.currentData.id;
            console.log($scope.currentData);
            if($scope.currentData.business_type!=3){
                $scope.business_status=0;
            }else{
                $scope.business_status=1;
            }
        });
        // 添加事项
        $scope.addNoticeForm=function(path){
            noticeServers.addNotice($scope.noticeData).then(function(data){
                if(data!==null){
                    $location.path('/Notice/index');
                }else{
                    alert("添加失败");
                }
            });     
        };
        // 邀约查询
        $scope.inviteSubmit=function(){
            var arr=[];
            arr[0]={
                id:$scope.currentData.id,
                invite:1,
                invite_time:$scope.currentData.invite_time
            };
            businessServers.setMultitermBusiness(arr).then(function(data){
                if(data){
                    $route.reload();
                }else{
                    alert("修改失败");
                }
            });
        };
        // 失败查询
        $scope.failureSubmit=function(){
            var arr=[];
            $scope.currentData = $filter('simplifyJsonFilter')(angular.copy($scope.currentData));
            $scope.currentData.business_type=4;
            var submitData={
                id:$scope.currentData.id,
                business_type:$scope.currentData.business_type,
                failure_reason_id:$scope.currentData.failure_reason_id,
                failure_reason_time:$scope.currentData.failure_reason_time};
            arr[0]=submitData;
            businessServers.setMultitermBusiness(arr).then(function(data){
                if(data){
                    $location.path('/Main/false_note');
                }else{
                    alert("修改失败");
                }
            });
        };
        // 上门时间查询
        $scope.visitSubmit=function(){
            var arr=[];
            arr[0]={
                id:$scope.currentData.id,
                visit:1,
                visit_time:$scope.currentData.visit_time
            };
            businessServers.setMultitermBusiness(arr).then(function(data){
                if(data){
                    $route.reload();
                }else{
                    alert("修改失败");
                }
            });
        };
        // 签约查询
        $scope.signSubmit=function(){
            var arr=[];
            arr[0]={
                id:$scope.currentData.id,
                sign:1,
                sign_time:$scope.currentData.sign_time
            };
            businessServers.setMultitermBusiness(arr).then(function(data){
                if(data){
                    $route.reload();
                }else{
                    alert("修改失败");
                }
            });
        };
        // 成交查询
        $scope.dealSubmit=function(){
            var arr=[];
            arr[0]={
                id:$scope.currentData.id,
                deal:1,
                deal_time:$scope.currentData.deal_time
            };
            businessServers.setMultitermBusiness(arr).then(function(data){
                if(data){
                    $route.reload();
                }else{
                    alert("修改失败");
                }
            });
        };
        // 报道查询
    	$scope.registrationSubmit=function(){
            var arr=[];
            arr[0]={
                id:$scope.currentData.id,
                registration:1,
                registration_time:$scope.currentData.registration_time,
             	business_type:5   
            };
            businessServers.setMultitermBusiness(arr).then(function(data){
                if(data){
                    $location.path('/Main/success_bus');
                }else{
                    alert("修改失败");
                }
            });
        };
        // 提交商机基本信息 
         $scope.customerForm=function(){
            var arr=[];
            if($scope.business_status==1){
                $scope.currentData.business_type=3;
            }else{
                if($scope.currentData.consultant_id!==""){
                    $scope.currentData.business_type=1;
                }else{
                    $scope.currentData.business_type=2;
                }
            }
            $scope.currentData.follow_up_records=angular.toJson($scope.currentData.follow_up_records);
            arr[0]=$filter('simplifyJsonFilter')($scope.currentData);
            businessServers.setMultitermBusiness(arr).then(function(data){
                if (data) {
                    $route.reload();
                }else{
                    alert("添加失败");
                }
            });
        };
        $scope.changeBusinessStatus=function(){
            $scope.business_status=$scope.business_status===0?1:0;
        };
        //添加跟进记录
        $scope.followRecords={};
        $scope.addFollowRecords=function(data){
            if(confirm('是否提交?')===false)
                return false;
            if(data){
                if ($scope.currentData.follow_up_records===null) {
                    $scope.currentData.follow_up_records=[];
                }
                $scope.currentData.follow_up_records.push($scope.followRecords);
                $scope.currentData.follow_up_records=$filter('orderBy')($scope.currentData.follow_up_records,'time',true);
                $scope.currentData.follow_up_records=angular.toJson($scope.currentData.follow_up_records);
                $scope.currentData = $filter('simplifyJsonFilter')($scope.currentData);
                $scope.submitData={id:$scope.currentData.id,follow_up_records:$scope.currentData.follow_up_records};
                $scope.arr=[$scope.submitData];
                businessServers.setMultitermBusiness($scope.arr).then(function(data){
                    if(data){
                        $route.reload();
                    }else{
                        alert("添加失败");
                    }
                });
            }else{
                alert("请填写全部信息");
            }

        };
        // 提交客户资料信息
        $scope.addMessage=function(data){
            var arr=[];
            $scope.formData.id=$scope.currentData.id;
            $scope.formData = $filter('simplifyJsonFilter')($scope.formData);
            arr[0]=$scope.formData;
            businessServers.setMultitermBusiness(arr).then(function(data){
                if(data){
                    $route.reload();
                }else{
                    alert("添加失败");
                }
            });
        };
    }]);
});